package com.vrp3d.common.utils;

import com.vrp3d.common.enums.CommonExceptionEnum;
import com.vrp3d.common.exceptions.CommonException;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 编码解码工具类
 * JDK提供的URLEncoder
 *
 * @author vrp3d
 */
@Slf4j
public class Encodes {

    /**
     * URL 编码, Encode默认为UTF-8.
     */
    public static String urlEncode(String part) {
        try {
            return URLEncoder.encode(part, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("编码异常");
            throw new CommonException(CommonExceptionEnum.CODE_EXCEPTION);
        }
    }

    /**
     * URL 解码, Encode默认为UTF-8.
     */
    public static String urlDecode(String part) {
        try {
            return URLDecoder.decode(part, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("解码异常");
            throw new CommonException(CommonExceptionEnum.DECODING_FAILED);
        }
    }

}
